
//
// !!!修改文件后调用 npm run buildJs 生成 global-utils-all.js
//

"use strict";

import { globalUtils } from './global-utils-common';

/* SOURCE-CODE-START */

/**
 * @memberof globalUtils
 * @description 合并多个对象的字段到新的对象上(浅度复制)
 * @param {(object[]|...object)} objectArray 
 * @returns {object}
 */
globalUtils.concatObjects = function (objectArray) {
  if (!(objectArray instanceof Array)) {
    objectArray = arguments;
  }

  var newObj = {};

  for (var index = 0; index < objectArray.length; index++) {
    var obj = objectArray[index];

    for (var key in obj) {
      newObj[key] = obj[key];
    }
  }

  return newObj;
};

/**
 * @memberof globalUtils
 * @description 合并多个对象的字段到新的对象上(深度复制)
 * @param {(object[]|...object)} objectArray 
 * @returns {object}
 */
globalUtils.mergeObjects = function (objectArray) {
  if (!(objectArray instanceof Array)) {
    objectArray = arguments;
  }

  var newArgs = [{}];

  for (var index = 0; index < objectArray.length; index++) {
    newArgs.push(objectArray[index]);
  }

  return _.merge.apply(null, newArgs);
};

/**
 * @memberof globalUtils
 * @description 复制并忽略空值的属性
 * @param {*} source 
 * @returns {*}
 */
globalUtils.ignoreObjectEmptyValue = function (source) {
  if (source === null) {
    return null;
  }

  var result;

  if (source instanceof Array) {
    result = [];

    source.forEach(function (value) {
      var newValue = globalUtils.ignoreObjectEmptyValue(value);
      result.push(newValue);
    });
  } else if (globalUtils.isObject(source)) {
    result = {};

    for (var key in source) {
      var value = source[key];

      if (value === null || value === undefined || value === '') {
        //
      } else if ((value instanceof Array) || globalUtils.isObject(value)) {
        result[key] = globalUtils.ignoreObjectEmptyValue(value);
      } else {
        result[key] = value;
      }
    }
  } else {
    result = source;
  }

  return result;
};

/**
 * @memberof globalUtils
 * @description 转换空字符串成 null 值
 * @param {*} source 
 * @returns {*}
 */
globalUtils.convertEmptyStringToNull = function (source) {
  if (source === null || source === '') {
    return null;
  }

  var result;

  if (source instanceof Array) {
    result = [];

    source.forEach(function (value) {
      var newValue = globalUtils.convertEmptyStringToNull(value);
      result.push(newValue);
    });
  } else if (globalUtils.isObject(source)) {
    result = {};

    for (var key in source) {
      var value = source[key];
      var newValue;

      if (value === null || value === '') {
        newValue = null;
      } else if ((value instanceof Array) || globalUtils.isObject(value)) {
        newValue = globalUtils.convertEmptyStringToNull(value);
      } else {
        newValue = value;
      }

      result[key] = newValue;
    }
  } else {
    result = source;
  }

  return result;
};

/* SOURCE-CODE-END */

export { globalUtils };
